import RemoveList from '@/utils/RemoveList';

/** 画板事件类型 */
export type CanvasEventType = "resize" | "click" | "mousemove";

/** 画板事件 */
export default class CanvasEvent {

	/** 元素 */
	private element: HTMLElement;

	/** 移除列表 */
	private removeList: RemoveList = new RemoveList();

	/**
	 * 构造函数
	 * @param element 元素
	 */
	public constructor(element: HTMLElement) {
		this.element = element;
	}

	/**
	 * 新增事件
	 * @param type 事件类型
	 * @param event 要新增的事件
	 */
	public Add(type: CanvasEventType, event: (...a: any) => void): () => void {
		const element = type === "resize" ? window : this.element;
		element.addEventListener(type, event);
		return this.removeList.Add(() => {
			element.removeEventListener(type, event);
		});
	}

	/** 移除 */
	public Remove(): void {
		this.removeList.Remove();
	}
}
